#include <iostream>
using namespace std;

class Student{
//将类下面的属性定义成public类型
//class默认private,而结构体默认public
public:
    string name;
    int age;
    Student() {
        //定义一个构造函数，初始化时候调用
        this->name = "xiao J";
        this->age = 18;
        cout<< "调用了Student的构造函数" <<endl;
    }
    Student(string name);
    Student(string name, int age);
    void toString() {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
    }
    //定义类外的函数
    string getName();
};

//public Student 意味着，StudentTmp继承了Student的全部
class StudentTmp : public Student {
public:
    StudentTmp();
    StudentTmp(string name);
    StudentTmp(string name, int age);
};

int main() {
    Student stu;
    stu.name = "xiao";
    stu.toString();
    cout << stu.getName() << endl;
    //调用构造函数进行初始化
    Student stu2("xiaoJ");
    stu2.toString();
    
    StudentTmp stuTmp2;
    stuTmp2.toString();
    StudentTmp stuTmp3("xiaoJi", 19);
    stuTmp2.toString();
    StudentTmp stuTmp4("xiaoJi");
}

//构造函数还可以通过:name(name) 方式，给name属性赋值
Student::Student(string name):name(name) {
    cout << "单参构造被调用" << endl;
}
Student::Student(string name,int age):name(name),age(age) {}
string Student::getName() {
    return name;
}

//这里如果StudentTmp没有构造函数，默认调用父类的无参构造函数
//如果StudentTmp有构造函数 则先调用父类的无参构造函数，再调用子类
StudentTmp::StudentTmp() {
    cout<< "调用了StudentTmp的构造函数" <<endl;
}

StudentTmp::StudentTmp(string name, int age) {
    //这个构造指向会是这样
    //1 调用父类的无参构造
    //2 调用我们这个显示调用的有参构造
    Student(name, age);
}
//如果我们只想让他调用父类的单参数构造，而不调用无参构造，可以这么写
StudentTmp::StudentTmp(string name):Student(name) {
}